<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>文档</title>
  <link href="./css/semantic/semantic.min.css" rel="stylesheet">
  <link href="./css/markdown.css" rel="stylesheet">

    <link href="./css/wtxynavigation.css" rel="stylesheet">
    <script src="./js/jquery.min.js"></script>
    <script type="text/javascript" src="./js/include.js"></script>
  <style>
    body{padding-top:0;background:#f7f7f7}
    body>.ui.container{width:100%;padding:0}
    #project-title{background:#6e6e6e;width:100%;line-height:3.572rem;color:#fff;font-size:1.2rem}
    #project-title .title-wrap{font-weight:700;padding:0 12px;width:100%;max-width:1366px!important;min-width:900px;margin:0 auto}
    #project-title .left,#project-title right{display:inline-block}
    #project-title .right{float:right}
    #project-title .right .wiki-actions .menu{left:auto;right:0;margin-top:.4em!important}
    #wiki-preview-container{width:100%;max-width:1366px!important;min-width:900px;padding:0;border:1px solid #e3e9ed;border-top:0}
    #wiki-preview>.ui.segment{margin-top:0;padding:1rem;padding-left:0;border:0}
    .hover-places{display:inline-block}
    #page-detail{width:60%;margin:0 auto}
    #page-detail .title{font-weight:700;font-size:24px;padding:12px 0;border-bottom:1px solid #ccc}
    #page-detail .content{margin-top:15px}
    .content p{word-wrap:break-word}
    #wiki-parent,#wiki-title,#wikis{display:none}
    .ui.grid{padding:0!important}
    #ex-all{cursor:pointer;color:#537c8d}
  </style>
</head>
<body>
  <div class="ui container">
    <div id="project-title">
      <div class="title-wrap">
        <div class="left">
          <i class="icon eye"></i>
          文档预览:
          mianmSDK
        </div>
        <div class="right">
          Export by Gitee
        </div>
      </div>
    </div>

    <div class="ui container" id="wiki-preview-container">
      <div id="wiki-preview">
        <div class="ui segment">
          <div id="page-detail" class="markdown-body">
            <div class='title'>3.通知NSNotificationCenter在哪个线程</div><div class='content'><h1>
<a id="通知nsnotificationcenter在哪个线程" class="anchor" href="#%E9%80%9A%E7%9F%A5nsnotificationcenter%E5%9C%A8%E5%93%AA%E4%B8%AA%E7%BA%BF%E7%A8%8B"></a>通知NSNotificationCenter在哪个线程?</h1>
<h2>
<a id="描述" class="anchor" href="#%E6%8F%8F%E8%BF%B0"></a>描述</h2>
<h3>
<a id="1-使用方法一" class="anchor" href="#1-%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95%E4%B8%80"></a>1. 使用方法一</h3>
<ol>
<li>发送方</li>
</ol>
<p>发送方说: 我发送了一个<code>kNotificationLoginSuccess</code>的通知哈,  接收者注意了!</p>
<pre lang="objc" class="objc"><code>
[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationLoginSuccess object:nil];


</code></pre>
<ol start="2">
<li>接收方</li>
</ol>
<p>接收方: 我要接受名字叫<code>kNotificationLoginSuccess</code>, 收到通知以后我要调用我自己的对象方法<code>userLoginNotificationSuccess</code></p>
<pre><code>
[[NSNotificationCenter  defaultCenter] addObserver:self

 selector:@selector(userLoginNotificationSuccess)

 name:kNotificationLoginSuccess

 object:nil];

</code></pre>
<h3>
<a id="2-使用方法二" class="anchor" href="#2-%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95%E4%BA%8C"></a>2. 使用方法二</h3>
<ol>
<li>发送方</li>
</ol>
<p>发送方说: 我发送了一个<code>kNotificationLoginSuccess</code>的通知哈,  接收者注意了!</p>
<pre lang="objc" class="objc"><code>
[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationLoginSuccess object:nil];


</code></pre>
<ol start="2">
<li>接收方</li>
</ol>
<p>接收方: 我接收<code>kNotificationLoginSuccess</code>通知, 谁发的不管, 在子线程执行通知方法</p>
<pre lang="objc" class="objc"><code>
self.observer = [[NSNotificationCenter defaultCenter] addObserverForName:kNotificationLoginSuccess
                                                      object:nil
                                                       queue:[NSOperationQueue new]
                                                  usingBlock:^(NSNotification * _Nonnull note) {
        
        NSLog(@"%s, %@", __func__, [NSThread currentThread]);
    }];

</code></pre>
<ul>
<li>返回值：通知实际添加到的observer，移除通知要移除这个对象</li>
<li>name参数：通知的名字</li>
<li>object：接收哪个对象发送的通知</li>
<li>queue：接收到通知的回调在哪个线程中调用，如果传mainQueue则通知在主线程回调，否则在子线程回调</li>
<li>usingBlock：接收到通知回调的block</li>
</ul>
<p>摘抄
作者：半岛夏天
链接：<a href="https://www.jianshu.com/p/208568075b4f">半岛夏天-简书连接</a></p>
<h1>
<a id="答" class="anchor" href="#%E7%AD%94"></a>答:</h1>
<blockquote>
<p>回答这个问题得分情况, 看你使用哪种方式接收通知.</p>
</blockquote>
<ol>
<li>使用不带block的指定方法的接收方式, 哪个线程发送通知, 回到通知接收方法就是哪个线程.</li>
<li>使用带block,且指定了接收线程的接收方式, 那就是不管发送的时候是哪个线程了.</li>
</ol></div>
          </div>
        </div>
      </div>
      </div>
  </div>
</body>
</html>